#include <linux/init.h>
#include <linux/module.h>
#include <linux/fs.h>
#include <linux/slab.h>
#include <asm/uaccess.h>


#include "scull.h"

static int scull_major = SCULL_MAJOR;
static int scull_minor = SCULL_MINOR;
static int scull_nr_devs = SCULL_NR_DEVS;
static int scull_quantum = SCULL_QUANTUM;
static int scull_qset = SCULL_QSET;
static dev_t dev;
static struct scull_dev *sculldev;

module_param(scull_major, int      , S_IRUGO);
module_param(scull_nr_devs, int      , S_IRUGO);
module_param(scull_quantum, int       , S_IRUGO);
module_param(scull_qset , int     , S_IRUGO);

int scull_trim(struct scull_dev *dev)
{
	struct scull_qset *next, *dptr;
	int qset = dev->qset;
	int i;
	for (dptr = dev->data; dptr; dptr = next) {
		if (dptr->data) {
			for (i = 0; i < qset; i++) 
				kfree(dptr->data[i]);
			kfree(dptr->data);
			dptr->data = NULL;
		}
		next = dptr->next;
		kfree(dptr);
	}

	dev->size = 0;
	dev->quantum = scull_quantum;
	dev->qset = scull_qset;
	dev->data = NULL;
	return 0;
}

static struct scull_qset *scull_follow(struct scull_dev *dev, int n)
{	
	while(n--) {
		if (!dev->data->next) {
			dev->data->next = 
				kmalloc(sizeof(struct scull_qset), GFP_KERNEL);
			memset(dev->data->next, 0, sizeof(struct scull_qset));
		}
		dev->data = dev->data->next;
		continue;
	}
	return dev->data;
}

static int scull_open(struct inode *inode, struct file *filp)
{
	struct scull_dev *dev;
	dev = container_of(inode->i_cdev, struct scull_dev, cdev);
	filp->private_data =dev;

	
	if ((filp->f_flags & O_ACCMODE) == O_WRONLY) {
		scull_trim(dev);
		dev->data = kmalloc(sizeof(struct scull_qset), GFP_KERNEL);
		memset(dev->data, 0, sizeof(struct scull_qset));
	}
	return 0;
}

static int scull_release(struct inode *inode, struct file *filp)
{
	return 0;
}

static ssize_t scull_read(struct file *filp, char __user *buf, size_t count, loff_t *f_pos)
{
	struct scull_dev *dev = filp->private_data;
	struct scull_qset *dptr;
	int quantum = dev->quantum, qset = dev->qset;
	int itemsize = quantum * qset;
	int item, s_pos, q_pos, rest;
	ssize_t retval = 0;
	
	if (down_interruptible(&dev->sem))
		return -ERESTARTSYS;
	if (*f_pos >= dev->size)
		goto out;
	if (*f_pos +count > dev->size)
		count = dev->size - *f_pos;

	item = (long)*f_pos / itemsize;
	rest = (long)*f_pos %itemsize;
	s_pos = rest / quantum; q_pos = rest % quantum;

	dptr = scull_follow(dev, item);

	if (dptr == NULL || !dptr->data ||!dptr->data[s_pos])
		goto out;

	if (count > quantum - q_pos)
		count = quantum - q_pos;

	if (copy_to_user(buf, dptr->data[s_pos] + q_pos, count)) {
		retval = -EFAULT;
		goto out;
	}
	printk(KERN_NOTICE "%s :count = %lu\n", __func__, (long unsigned int)count);
	*f_pos += count;
	retval = count;
	out:
		up(&dev->sem);
		return retval;
}

static ssize_t scull_write(struct file *filp, const char __user *buf, size_t count,
			loff_t *f_pos)

{
	struct scull_dev *dev = filp->private_data;
	struct scull_qset *dptr;
	int quantum = dev->quantum, qset = dev->qset;
	int itemsize = quantum * qset;
	int item, s_pos, q_pos, rest;
	ssize_t retval = -ENOMEM;

	if (down_interruptible(&dev->sem))
		return -ERESTARTSYS;
	item = (long)*f_pos / itemsize;
	rest = (long)*f_pos % itemsize;
	s_pos = rest / quantum;
	q_pos = rest % quantum;

	dptr = scull_follow(dev, item);

	if (dptr == NULL)
		goto out;

	if (!dptr->data) {
		dptr->data = kmalloc(qset * sizeof(char *), GFP_KERNEL);
		if (!dptr->data)
			goto out;
		memset(dptr->data, 0, qset * sizeof(char *));
	}

	if (!dptr->data[s_pos]) {
		dptr->data[s_pos] = kmalloc(quantum, GFP_KERNEL);
		if (!dptr->data[s_pos])
			goto out;
	}

	if (count > quantum - q_pos)
		count = quantum - q_pos;

	if (copy_from_user(dptr->data[s_pos] + q_pos, buf, count)) {
		retval = -EFAULT;
		goto out;
	}

	*f_pos += count;
	retval = count;

	if (dev->size < *f_pos)
		dev->size = *f_pos;
	printk(KERN_NOTICE "%s: size = %lu, write count = %lu\n", __func__, 
		(long unsigned int)dev->size, (long unsigned int)count);
	out:
		up(&dev->sem);
		return retval;
	
}

static struct file_operations scull_fops = {
	.owner = THIS_MODULE,
	
	//.llseek = scull_llseek,
	.read = scull_read,
	.write = scull_write,
	//.ioctl = scull_ioctl,
	.open = scull_open,
	.release = scull_release,
	
};

/*
* Assigned dev number.
*/
static int scull_register_chrdev(void)
{
	int result;
	if (scull_major) {
		dev = MKDEV(scull_major, scull_minor);
		result = register_chrdev_region(dev, scull_nr_devs, "scull");
	} else {
		result = alloc_chrdev_region(&dev, scull_minor, scull_nr_devs, 
					     "scull");
		scull_major = MAJOR(dev);
	}
	
	return result;
}

/*
* Scull character device registration.
*/
static void scull_setup_cdev(struct scull_dev *dev, int index)
{
	int err, devno = MKDEV(scull_major, scull_minor + index);
	cdev_init(&dev->cdev, &scull_fops);
	dev->cdev.owner = THIS_MODULE;
	dev->cdev.ops = &scull_fops;
	err = cdev_add(&dev->cdev, devno, 1);
	if (err)
		printk(KERN_WARNING "Error %d adding scull%d", err, index);
}


static void scull_exit(void)
{
	int i;	
	if (sculldev) {
		for (i = 0; i < scull_nr_devs; i++) {
			scull_trim(&sculldev[i]);
			cdev_del(&sculldev[i].cdev);
		}
		kfree(sculldev);
	}

	unregister_chrdev_region(dev, scull_nr_devs);
}


static int __init scull_init(void)
{
	int result = 0;
	int i;
	result = scull_register_chrdev();
	if (result < 0) {
		printk(KERN_WARNING "scull: can't get major %d\n", scull_major);
		return result;
	}
	printk(KERN_NOTICE "%s:Get dev major number = %d\n", 
				__func__, scull_major);

	sculldev = kmalloc(scull_nr_devs * sizeof(struct scull_dev), GFP_KERNEL);
	if (!sculldev) {
		result = -ENOMEM;
		goto fail;
	}

	for (i = 0; i < scull_nr_devs; i++) {

		sculldev[i].data = kmalloc(sizeof(struct scull_qset), GFP_KERNEL);
		memset(sculldev[i].data, 0, sizeof(struct scull_qset));
		sculldev[i].quantum = scull_quantum;
		sculldev[i].qset = scull_qset;
		sculldev[i].size = 0;
		sculldev[i].access_key = 0;
		sema_init(&sculldev[i].sem, 1);
		scull_setup_cdev(&sculldev[i], i);
	}

fail:
	if (result)
		scull_exit();	
	return result;
	
}

module_init(scull_init);
module_exit(scull_exit);

MODULE_LICENSE("GPL");
MODULE_VERSION("v1.0");
MODULE_AUTHOR("LiuGuiChao");
MODULE_DESCRIPTION("A test moudle!");

